home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 60914 / 60914.xpi / chrome / content / setinterval.js < prev    next >
Text File  |  2010-01-24  |  11KB  |  308 lines

  1.  
  2.     var PersonasRotatorSetInterval = {
  3.     
  4.         STRINGS:null,
  5.         
  6.         getlibraryfileURI : function () {
  7.             
  8.             var file = Components.classes["@mozilla.org/file/directory_service;1"]
  9.                      .getService(Components.interfaces.nsIProperties)
  10.                      .get("ProfD", Components.interfaces.nsIFile);            
  11.             
  12.             file.append("personasrotator");
  13.             
  14.             file.append("library.rdf");
  15.             
  16.             var ios = Components.classes["@mozilla.org/network/io-service;1"]
  17.                                 .getService(Components.interfaces.nsIIOService);
  18.                                 
  19.             var URL = ios.newFileURI(file);
  20.             
  21.             return URL.spec;
  22.         
  23.             
  24.         },
  25.  
  26.         populateItems : function () {
  27.                         
  28.             var ds=PersonasRotatorRDF.rdfService.GetDataSourceBlocking(PersonasRotatorSetInterval.getlibraryfileURI());    
  29.  
  30.             var container=PersonasRotatorRDF.rdfContainerUtils.MakeSeq(ds,PersonasRotatorRDF.rdfService.GetResource("urn:personasrotator:library"));
  31.             
  32.             var genericpersonasseqenumurator=container.GetElements();
  33.             
  34.             var namePredicate = PersonasRotatorRDF.rdfService.GetResource("urn:personasrotator#name");
  35.  
  36.             while (genericpersonasseqenumurator.hasMoreElements()) {
  37.         
  38.                 var collection=genericpersonasseqenumurator.getNext();
  39.                 
  40.                 var nametarget=ds.GetTarget(collection,namePredicate,true);
  41.                 
  42.                 if (nametarget) {
  43.                     
  44.                     var name = nametarget.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  45.  
  46.                 }
  47.  
  48.                 var newmenuitem=document.createElement("menuitem");
  49.  
  50.                 newmenuitem.setAttribute("label",name);
  51.                 
  52.                 newmenuitem.setAttribute("id",name+"menuitem");
  53.             
  54.                 newmenuitem.setAttribute("class","collection-menuitem");
  55.                 
  56.                 document.getElementById("collistpopup").appendChild(newmenuitem);
  57.         
  58.             }
  59.  
  60.         },
  61.         
  62.         cancelDialog : function () {
  63.  
  64.             return true;
  65.             
  66.         },    
  67.         
  68.         acceptDialog : function () {
  69.  
  70.             var choseninterval=document.getElementById("setintervaltimelist").value;
  71.             var retrievedSeconds=PersonasRotatorSetInterval.retrieveIntervalSeconds(choseninterval);
  72.                 
  73.             var chosencategory=document.getElementById("rotationcategorylist").value;
  74.             var retrievedCategory=chosencategory;
  75.             
  76.             var personasrotatorprefsinstance = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  77.             personasrotatorprefsinstance.setIntPref("extensions.personasrotator.rotationinterval",retrievedSeconds);
  78.             personasrotatorprefsinstance.setCharPref("extensions.personasrotator.rotationcategory",retrievedCategory);
  79.             //if saved then rotation should begin
  80.             personasrotatorprefsinstance.setBoolPref("extensions.personasrotator.rotationenabled",true);
  81.             
  82.             //updateRotationInterval is true method. Do not change it.
  83.             window.opener.PersonasRotator.updateRotationInterval(retrievedSeconds);
  84.         
  85.             return true;
  86.  
  87.         },
  88.         
  89.         retrieveIntervalSeconds:function(passedInterval){
  90.  
  91.             if(passedInterval=="0") return 60;
  92.             else if(passedInterval=="1") return 120;
  93.             else if(passedInterval=="2") return 300;
  94.             else if(passedInterval=="3") return 600;
  95.             else if(passedInterval=="4") return 900;
  96.             else if(passedInterval=="5") return 1200;
  97.             else if(passedInterval=="6") return 1800;
  98.             else if(passedInterval=="7") return 3600;
  99.             else if(passedInterval=="8") return parseInt(document.getElementById("customtime").getAttribute("customtime"));
  100.         
  101.         },
  102.         
  103.         retrieveIntervalNumber:function(currentInterval){
  104.         
  105.             if(currentInterval==60) return 0;
  106.             else if(currentInterval==120) return 1;
  107.             else if(currentInterval==300) return 2;
  108.             else if(currentInterval==600) return 3;
  109.             else if(currentInterval==900) return 4;
  110.             else if(currentInterval==1200) return 5;
  111.             else if(currentInterval==1800) return 6;
  112.             else if(currentInterval==3600) return 7;
  113.             else return 8;
  114.         
  115.         },
  116.             
  117.         retrieveCategoryName:function(currentCategory){
  118.         
  119.             if(currentCategory=="All") return 0;
  120.             else if(currentCategory=="My Favorites") return 1;
  121.             else if(currentCategory=="Abstract") return 2;
  122.             else if(currentCategory=="Causes") return 3;
  123.             else if(currentCategory=="Fashion") return 4;
  124.             else if(currentCategory=="Film and TV") return 5;
  125.             else if(currentCategory=="Firefox") return 6;
  126.             else if(currentCategory=="Foxkeh") return 7;
  127.             else if(currentCategory=="Holiday") return 8;
  128.             else if(currentCategory=="Music") return 9;
  129.             else if(currentCategory=="Nature") return 10;
  130.             else if(currentCategory=="Other") return 11;
  131.             else if(currentCategory=="Scenery") return 12;
  132.             else if(currentCategory=="Solid") return 13;
  133.             else if(currentCategory=="Sports") return 14;
  134.             else if(currentCategory=="Websites") return 15;
  135.             else return 0; //for most popular and news bugs
  136.             
  137.         },
  138.         
  139.         handleWindowLoad :function(event){
  140.  
  141.             PersonasRotatorSetInterval.STRINGS = document.getElementById("setintervalBundle");
  142.             
  143.             document.getElementById("savesetintervalbutton").addEventListener("command",PersonasRotatorSetInterval.handleSetIntervalButtonCommand,false);
  144.             
  145.             document.getElementById("cancelsetintervalbutton").addEventListener("command",PersonasRotatorSetInterval.cancelSetIntervalButtonCommand,false);
  146.             
  147.             var personasrotatorprefsinstance = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  148.             
  149.             var personasrotatorrotationinterval = personasrotatorprefsinstance.getIntPref("extensions.personasrotator.rotationinterval");
  150.             
  151.             var personasrotatorrotationcategory = personasrotatorprefsinstance.getCharPref("extensions.personasrotator.rotationcategory");
  152.  
  153.             document.getElementById('intervalsecondstext').firstChild.nodeValue='Interval is '+personasrotatorrotationinterval+' seconds';
  154.             
  155.             document.getElementById("setintervaltimelist").selectedIndex=PersonasRotatorSetInterval.retrieveIntervalNumber(personasrotatorrotationinterval);
  156.  
  157.             document.getElementById("rotationcategorylist").selectedIndex=PersonasRotatorSetInterval.retrieveCategoryName(personasrotatorrotationcategory);
  158.             
  159.             //selindex is needed for custom revertback
  160.             document.getElementById("setintervaltimelist").setAttribute("selindex",document.getElementById("setintervaltimelist").selectedIndex);
  161.             document.getElementById("rotationcategorylist").setAttribute("catselindex",document.getElementById("rotationcategorylist").selectedIndex);
  162.             
  163.             //when custom value is saved with no change
  164.             document.getElementById('customtime').setAttribute('customtime',personasrotatorrotationinterval);
  165.             
  166.         },
  167.         
  168.         handleSetIntervalButtonCommand:function(event){
  169.         
  170.             if(PersonasRotatorSetInterval.acceptDialog()) window.close();
  171.         
  172.         },
  173.         
  174.         cancelSetIntervalButtonCommand:function(event){
  175.         
  176.             window.close();
  177.         
  178.         },
  179.         
  180.         onCategoryChange:function(event){
  181.         
  182.             if(event.currentTarget.value=='My Favorites') {
  183.                 
  184.                 if (!(window.opener.PersonaService.favorites && window.opener.PersonaService.favorites.length > 0 &&window.opener.PersonaService.isUserSignedIn)) {
  185.                     
  186.                     //revert back. confirm is lagging revertion back. so it is put before confirm
  187.                     event.currentTarget.selectedIndex=event.currentTarget.getAttribute("catselindex");                
  188.  
  189.                     if(confirm("Rotation of My Favorites requires logging in the system. Would you like to log in now?")) window.opener.gBrowser.selectedTab=window.opener.gBrowser.addTab("http://www.getpersonas.com/signin?return=/gallery/All/Favorites");
  190.                     
  191.                 }
  192.             
  193.             }
  194.             
  195.             else {
  196.  
  197.                 event.currentTarget.setAttribute("catselindex",event.currentTarget.selectedIndex);
  198.             
  199.             }            
  200.         
  201.         },
  202.         
  203.         onIntervalChange:function(event){
  204.         
  205.             if(event.currentTarget.value!='8') {
  206.                 
  207.                 document.getElementById('intervalsecondstext').firstChild.nodeValue='Interval is '+PersonasRotatorSetInterval.retrieveIntervalSeconds(event.currentTarget.selectedItem.getAttribute('value'))+" seconds";
  208.                 event.currentTarget.setAttribute("selindex",event.currentTarget.selectedIndex);
  209.             
  210.             }
  211.             
  212.             else {
  213.             
  214.             var promptwarning=PersonasRotatorSetInterval.STRINGS.getString("setInSeconds");
  215.             
  216.             var customInterval="";
  217.             
  218.             function validate(passedcustomInterval) {
  219.                 
  220.                 customInterval=passedcustomInterval;
  221.                 
  222.                 if (customInterval==null) return null;
  223.                 
  224.                 customInterval=customInterval.replace(/^\s+|\s+$/g,'');
  225.                 
  226.                 while (customInterval=="") {
  227.                 
  228.                 customInterval=PersonasRotatorSetInterval.createPromptPrompt(PersonasRotatorSetInterval.STRINGS.getString("emptyWarning"));
  229.                     
  230.                     if (customInterval==null) return null;
  231.                     
  232.                     customInterval=customInterval.replace(/^\s+|\s+$/g,'');
  233.                 
  234.                 }
  235.                 
  236.                 while (!isNaN(customInterval)&& parseInt(customInterval) < 60) {
  237.                     
  238.                     customInterval=PersonasRotatorSetInterval.createPromptPrompt(PersonasRotatorSetInterval.STRINGS.getString("lessThan60"));
  239.                     
  240.                     if (customInterval==null) return null;
  241.                     
  242.                     customInterval=customInterval.replace(/^\s+|\s+$/g,'');
  243.                     
  244.                     while (customInterval=="") {
  245.                     
  246.                         customInterval=PersonasRotatorSetInterval.createPromptPrompt(PersonasRotatorSetInterval.STRINGS.getString("setInSeconds"));
  247.                     
  248.                         if (customInterval==null) return null;
  249.                         
  250.                         customInterval=customInterval.replace(/^\s+|\s+$/g,'');
  251.                     }
  252.                     
  253.                 }        
  254.                     
  255.                 return customInterval;            
  256.                 
  257.             }
  258.  
  259.             function checkTextualCollision(passedcustomInterval){
  260.                 
  261.                 if (passedcustomInterval==null) return false;
  262.                 
  263.                 if(isNaN(passedcustomInterval)) return true;
  264.                 
  265.                 return false;
  266.  
  267.             }
  268.  
  269.             while(checkTextualCollision(validate(PersonasRotatorSetInterval.createPromptPrompt(promptwarning)))){
  270.             
  271.                 promptwarning=PersonasRotatorSetInterval.STRINGS.getString("setInNumber");
  272.             
  273.             };
  274.         
  275.             if (customInterval==null) {
  276.                 
  277.                 //revertback
  278.                 event.currentTarget.selectedIndex=event.currentTarget.getAttribute("selindex");                
  279.                 return;                
  280.             
  281.             }            
  282.             
  283.             document.getElementById('intervalsecondstext').firstChild.nodeValue='Interval is '+customInterval+' seconds';
  284.             document.getElementById('customtime').setAttribute('customtime',customInterval);
  285.             event.currentTarget.setAttribute("selindex",event.currentTarget.selectedIndex);
  286.             
  287.             }        
  288.         
  289.         },
  290.                 
  291.         createPromptPrompt:function(promptString){
  292.             
  293.             var prompt = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  294.             
  295.             var input = {value:""}; 
  296.             
  297.             var check = {value:false};
  298.  
  299.             var result= prompt.prompt(null,"Personas Rotator",promptString,input,null,check);
  300.             
  301.             if(result==false) return null;
  302.             else return input.value;
  303.             
  304.         }
  305.  
  306.     }
  307.             
  308.     window.addEventListener("load",PersonasRotatorSetInterval.handleWindowLoad,false);